/**
 * 
 */
package com.keggview.application.treeviewer.ui;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;

import com.keggview.application.datatypes.Base;
import com.keggview.application.datatypes.Pathway;
import com.keggview.application.datatypes.Registry;

/**
 * @author Pablo
 *
 */
public class TreeViewContentProvider implements ITreeContentProvider {

	private static Object[] EMPTY = new Object[0];
	protected TreeViewer viewer;
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
	 */
	@Override
	public Object[] getChildren(Object parentElement) {

		if (parentElement instanceof Registry) {
			return Registry.getAll().toArray();
		}
		return EMPTY;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
	 */
	@Override
	public Object getParent(Object element) {
//		// TODO Auto-generated method stub
//		return null;
		if (element instanceof Base) {
			Base nBase = (Base) element;
			return nBase;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
	 */
	@Override
	public boolean hasChildren(Object element) {
//		return false;
		if (element instanceof Pathway) {
			return true;
		} else
			return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
	 */
	@Override
	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		return getChildren(inputElement);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	@Override
	public void dispose() {

	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		this.viewer = (TreeViewer) viewer;
		if (oldInput != null) {
			removeListenerFrom((Base) oldInput);
		}
		if (newInput != null) {
			addListenerTo((Base) newInput);
		}
	}
	
	protected void removeListenerFrom(Base item) {
		;
	}
	
	protected void addListenerTo(Base item) {
		;
	}
}
